﻿package Modules.PlayerModule.InventoryModule.Tables
{
	import Modules.CommunicationsModule.ClientServerCommunications;
	import Modules.MainTimeLine;
		
	public class ItemTable
	{
		public var seedTable:Array;
		public var buildingTable:Array;
		public var decorationTable:Array;
		public var foodTable:Array;
		public var expansionTable:Array;
		
		public function ItemTable(){
			
		}
		
		public function saveStorage():Array{
			
			var storage:Array = [];
			var item:Item;
			
			this.plusFoodItemAmount("f1",5);
			this.plusFoodItemAmount("f2",5);
			this.plusFoodItemAmount("f3",5);
			this.plusFoodItemAmount("f4",5);
			this.plusFoodItemAmount("f5",5);
			this.plusFoodItemAmount("f6",5);
			
			for each(item in seedTable){
				
				if(item.getAmount() > 0){
					
					var anItem:Object = new Object();
					anItem.id = item.getID();
					anItem.amount = item.getAmount();
					storage.push(anItem);	
				}
			}
			
			for each(item in buildingTable){
				
				if(item.getAmount() > 0){
					
					var anItem2:Object = new Object();
					anItem2.id = item.getID();
					anItem2.amount = item.getAmount();
					storage.push(anItem2);	
				}
			}
			
			for each(item in decorationTable){
				
				if(item.getAmount() > 0){
					
					var anItem3:Object = new Object();
					anItem3.id = item.getID();
					anItem3.amount = item.getAmount();
					storage.push(anItem3);	
				}
			}
			
			for each(item in foodTable){
				
				if(item.getAmount() > 0){
					
					var anItem4:Object = new Object();
					anItem4.id = item.getID();
					anItem4.amount = item.getAmount();
					storage.push(anItem4);	
				}
			}
			
			return storage;
		}
	
		public function loadTable():void{
			
			ClientServerCommunications.loadItemTable();
		}
		
		//load seed table from database and fill array with seedItems
		public function setSeedTable(itemArray:Array):void{
			
			seedTable = [];
			
			var length:int = itemArray.length;
			var i:int=0, currentItem:Object;
			
			for(i; i<length; i++){
			
				currentItem = itemArray[i];
				seedTable.push(new SeedItem(currentItem.itemId,currentItem.name,"seed",currentItem.gold));
				(seedTable[i] as SeedItem).expgain = currentItem.expGained;
				(seedTable[i] as SeedItem).levelRequired = currentItem.levelRequired;
				(seedTable[i] as SeedItem).sellAmount = currentItem.sellsForGold;
				(seedTable[i] as SeedItem).timeToGrow = currentItem.timeToGrow;
			}
			
			MainTimeLine.loadedInventory +=1;
			
			if(MainTimeLine.loadedInventory ==4){
				ClientServerCommunications.loadPlayerInventory(MainTimeLine.session.player.playerID);
			}
			
		}
		
		//load seed table from database and fill array with buildingItems
		public function setBuildingTable(itemArray:Array):void{
			
			buildingTable = [];
			
			var length:int = itemArray.length;
			var i:int=0, currentItem:Object;
			
			for(i; i<length; i++){
				
				currentItem = itemArray[i];
				buildingTable.push(new BuildingItem(currentItem.itemId,currentItem.name,"building",currentItem.gold));
				(buildingTable[i] as BuildingItem).expgain = currentItem.expGained;
				(buildingTable[i] as BuildingItem).levelRequired = currentItem.levelRequired;
				(buildingTable[i] as BuildingItem).staminaGained = currentItem.staminaGained;
				(buildingTable[i] as BuildingItem).staminaGainedPerHour = currentItem.staminaGainedPerHour;
				(buildingTable[i] as BuildingItem).diamondPrice = currentItem.diamond;
				(buildingTable[i] as BuildingItem).seedBags = currentItem.seedBags;
				(buildingTable[i] as BuildingItem).eggs = currentItem.eggs;
				(buildingTable[i] as BuildingItem).milkBottles = currentItem.milkBottles;
				(buildingTable[i] as BuildingItem).growthTimeReduced = currentItem.growthTimeReduced;
				(buildingTable[i] as BuildingItem).description = currentItem.description;
			}
			
			MainTimeLine.loadedInventory +=1;
			
			if(MainTimeLine.loadedInventory ==4){
				ClientServerCommunications.loadPlayerInventory(MainTimeLine.session.player.playerID);
			}
		}
		
		//load deco table from database and fill array with decorationItems
		public function setDecorationTable(itemArray:Array):void{
			
			decorationTable = [];
			
			var length:int = itemArray.length;
			var i:int=0, currentItem:Object;
			
			for(i; i<length; i++){
				currentItem = itemArray[i];
				decorationTable.push(new DecorationItem(currentItem.itemId,currentItem.name,"deco",currentItem.gold));
			}
			
			MainTimeLine.loadedInventory +=1;
			
			if(MainTimeLine.loadedInventory ==4){
				ClientServerCommunications.loadPlayerInventory(MainTimeLine.session.player.playerID);
			}
		}
		
		//load seed table from database and fill array with foodItems
		public function setFoodTable(itemArray:Array):void{
			
			foodTable = [];
			
			var length:int = itemArray.length;
			var i:int=0, currentItem:Object;
			
			for(i; i<length; i++){
				
				currentItem = itemArray[i];
				foodTable.push(new FoodItem(currentItem.itemId,currentItem.name,"food",currentItem.gold));
				(foodTable[i] as FoodItem).expgain = currentItem.expGained;
				(foodTable[i] as FoodItem).levelRequired = currentItem.levelRequired;
				(foodTable[i] as FoodItem).replenishedStamina = currentItem.replenishedStamina;
			}
			
			MainTimeLine.loadedInventory +=1;
			
			if(MainTimeLine.loadedInventory ==4){
				ClientServerCommunications.loadPlayerInventory(MainTimeLine.session.player.playerID);
			}
		}
		
		//load seed table from database and fill array with expansion details
		public function setExpansionTable(itemArray:Array):void{
			
			expansionTable = [];
			
			var length:int = itemArray.length;
			var i:int=0, currentItem:Object;
			
			for(i; i<length; i++){
				
				currentItem = itemArray[i];
				expansionTable.push(new ExpansionItem(currentItem.itemId,currentItem.name,"expansion",currentItem.gold));
				(expansionTable[i] as ExpansionItem).expgain = currentItem.expGained;
				(expansionTable[i] as ExpansionItem).levelRequired = currentItem.levelRequired;
				(expansionTable[i] as ExpansionItem).pals = currentItem.pals;
			}
			
			MainTimeLine.loadedInventory +=1;
			
			if(MainTimeLine.loadedInventory ==5){
				ClientServerCommunications.loadPlayerInventory(MainTimeLine.session.player.playerID);
			}
		}
		
		//accessor functions
		public function getSeedTable():Array{
			
			return seedTable;
		}
		
		public function getBuildingTable():Array{
			
			return buildingTable;
		}
		
		public function getDecorationTable():Array{
			
			return decorationTable;
		}
		
		public function getFoodTable():Array{
		
			return foodTable;
		}
		
		public function getExpansionTable():Array{
			
			return expansionTable;
		}
		
		//allows setting of the quantities of the specified amount of the specified item.
		public function setItemAmount(itemID:String, amount:int):void{
			
			switch(itemID.charAt(0)){
				
				case 's':
					setSeedItemAmount(itemID, amount);
					break;
				
				case 'b':
					setBuildingItemAmount(itemID, amount);
					break;
				
				case 'f':
					setFoodItemAmount(itemID, amount);
					break;
				
				case 'd':
					setDecorationItemAmount(itemID, amount);
					break;
				
				case 'e':
					setExpansionItemAmount(itemID, amount);	
					break;
				
				default:
			}
		}
		
		private function setSeedItemAmount(itemID:String, amount:int):void{
			
			var length:int = seedTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (seedTable[i] as Item).getID()){
					(seedTable[i] as Item).setAmount(amount);
				}
			}
		}
		
		private function setBuildingItemAmount(itemID:String, amount:int):void{
			
			var length:int = buildingTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (buildingTable[i] as Item).getID()){
					(buildingTable[i] as Item).setAmount(amount);
				}
			}
		}
		
		private function setFoodItemAmount(itemID:String, amount:int):void{
			
			var length:int = foodTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (foodTable[i] as Item).getID()){
					(foodTable[i] as Item).setAmount(amount);
				}
			}
		}
		
		private function setDecorationItemAmount(itemID:String, amount:int):void{
			
			var length:int = decorationTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (decorationTable[i] as Item).getID()){
					(decorationTable[i] as Item).setAmount(amount);
				}
			}
		}
		
		private function setExpansionItemAmount(itemID:String, amount:int):void{
			
			var length:int = expansionTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (expansionTable[i] as Item).getID()){
					(expansionTable[i] as Item).setAmount(amount);
				}
			}
		}
		
		//increases adds the amount to the quantity of an item. the amount and be positive or negative
		public function plusItemAmount(itemID:String, amount:int):Boolean{
			
			switch(itemID.charAt(0)){
				
				case 's':
					return plusSeedItemAmount(itemID, amount);
					break;
				
				case 'b':
					return plusBuildingItemAmount(itemID, amount);
					break;
				
				case 'f':
					return plusFoodItemAmount(itemID, amount);
					break;
				
				case 'd':
					return plusDecorationItemAmount(itemID, amount);
					break;
				
				case 'e':
					return plusExpansionItemAmount(itemID, amount);	
					break;
				
				default:
					
					return false;
			}
		}
		
		private function plusSeedItemAmount(itemID:String, amount:int):Boolean{
			
			var length:int = seedTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (seedTable[i] as Item).getID()){
					
					if(amount>0){
						
						while(amount >0){
							(seedTable[i] as Item).upAmount();
							amount--;
						}
						
						return true;
					}else{
						
						while(amount < 0){
							
							if((seedTable[i] as Item).downAmount()){
								amount++;
								return true;
							}else{
								amount =0;	
							}
						}
						
						return false;
					}
				}
			}
			
			return true;
		}
		
		private function plusBuildingItemAmount(itemID:String, amount:int):Boolean{
			
			var length:int = buildingTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (buildingTable[i] as Item).getID()){
					
					if(amount>0){
						
						while(amount >0){
							(buildingTable[i] as Item).upAmount();
							amount--;
						}
						
						return true;
					}else{
						
						while(amount < 0){
							
							if((buildingTable[i] as Item).downAmount()){
								amount++;
								return true;
							}else{
								amount =0;	
							}
						}
						
						return false;
					}
				}
			}
			
			return true;
		}
		
		private function plusDecorationItemAmount(itemID:String, amount:int):Boolean{
			
			var length:int = decorationTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (decorationTable[i] as Item).getID()){
					
					if(amount>0){
						
						while(amount >0){
							(decorationTable[i] as Item).upAmount();
							amount--;
						}
						
						return true;
					}else{
						
						while(amount < 0){
							
							if((decorationTable[i] as Item).downAmount()){
								amount++;
								return true;
							}else{
								amount =0;	
							}
						}
						
						return false;
					}
				}
			}
			
			return true;
		}
		
		private function plusFoodItemAmount(itemID:String, amount:int):Boolean{
			
			var length:int = foodTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (foodTable[i] as Item).getID()){
					
					if(amount>0){
						while(amount >0){
							(foodTable[i] as Item).upAmount();
							amount--;
						}
						
						return true;
					}else{
						
						while(amount < 0){
							if((foodTable[i] as Item).downAmount()){
								amount++;
								return true;
							}else{
								amount =0;	
							}
						}
						
						return false;
					}
				}
			}
			
			return false;
		}
		
		public function getItem(itemID:String):Item{
			
			switch(itemID.charAt(0)){
				
				case 's':
					return searchTable(seedTable, itemID);
					break;
				
				case 'b':
					return searchTable(buildingTable, itemID);
					break;
				
				case 'f':
					return searchTable(foodTable, itemID);
					break;
				
				case 'd':
					return searchTable(decorationTable, itemID);
					break;
				
				case 'e':
					return searchTable(expansionTable, itemID);
					break;
				
				default:
					
					return null;
			}
		}
		
		private function searchTable(array:Array, id:String):Item{
			
			for(var index in array){
				
				if((array[index] as Item).getID() == id){
					return array[index];	
				}
			}
			
			return null;
			
		}
		
		private function plusExpansionItemAmount(itemID:String, amount:int):Boolean{
			
			var length:int = expansionTable.length;
			var i:int;
			
			for(i=0; i<length; i++){
				
				if(itemID == (expansionTable[i] as Item).getID()){
					
					if(amount>0){
						
						while(amount >0){
							(expansionTable[i] as Item).upAmount();
							amount--;
						}
						
						return true;
					}else{
						
						while(amount < 0){
							
							if((expansionTable[i] as Item).downAmount()){
								amount++;
								return true;
							}else{
								amount =0;	
							}
						}
						
						return false;
					}
				}
			}
			
			return true;
		}
	}
}